// 投资管理
var Investment = Ext.data.Record.create([
	{name : 'tid',mapping : 'tid',type : 'int'}, 
	{name : 'pid',mapping : 'fid',type : 'int'}, 
	{name : 'cnumber',mapping : 'cnumber',type : 'string'}, 
	{name : 'cardnumber',mapping : 'cardnumber',type : 'string'}, 
	{name : 'csum',mapping : 'csum',type : 'float'},  
	{name : 'loanid',mapping : 'loanid',type : 'int'}, 
	{name : 'loanname',mapping : 'loanname',type : 'string'},  
	{name : 'rate',mapping : 'rate',type : 'float'}, 
	{name : 'interest',mapping : 'interest',type : 'float'},
	{name : 'charge',mapping : 'charge',type : 'float'},
	{name : 'debtorid',mapping : 'debtorid',type : 'int'},
	{name : 'debtorname',mapping : 'debtorname',type : 'string'},
	{name : 'startdate',mapping : 'startdate',type : 'date'},
	{name : 'closedate',mapping : 'closedate',type : 'date'},
	{name : 'accountid',mapping : 'accountid',type : 'int'},
	{name : 'accountname',mapping : 'accountname',type : 'string'},
	{name : 'status',mapping : 'status',type : 'string'},
	{name : 'createtime',mapping : 'createtime',type : 'string'},
	{name : 'balance',mapping : 'balance',type : 'float'},
	{name : 'delaydays',mapping : 'delaydays',type : 'int'},
	{name : 'delaycost',mapping : 'delaycost',type : 'float'},
	{name : 'memo',mapping : 'memo',type : 'string'}
	]);

var cm_investment = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
	header : '合同号',
	width : 120,
	dataIndex : 'cnumber'
},{
	header : '借款人',
	width : 80,
	dataIndex : 'loanname',
	sortable : false
},{
	header : '借款日期',
	width : 90,
	dataIndex : 'startdate',
	renderer: renderDate,
	sortable:true
	
},{
	header : '到期日期',
	width : 90,
	dataIndex : 'closedate',
	renderer: renderDate,
	sortable:true
	
},{
	header : '借款总额',
	width : 80,
	dataIndex : 'csum',
	sortable : false
},{
	header : '利率%',
	width : 60,
	dataIndex : 'rate',
	sortable : true
},{
	header : '利息收入',
	width : 60,
	dataIndex : 'interest',
	sortable : true
},{
	header : '投资费用',
	width : 60,
	dataIndex : 'charge',
	sortable : true
},{
	header : '状态',
	width : 60,
	dataIndex : 'status',
	sortable : true
},{
	header : '逾期天数',
	width : 60,
	css : 'color=red',
	dataIndex : 'delaydays',
	renderer:renderDelay,
	sortable : true
},{
	header : '逾期利息',
	width : 60,
	dataIndex : 'delaycost',
	renderer:renderDelay,
	sortable : true
},{
	header : '归还日期',
	width : 90,
	dataIndex : 'updatetime',
	renderer: renderDate,
	sortable : true
}]);

cm_investment.defaultSortable = true;

var btn_add_investment = new Ext.Button({
	text : '增加借款',
	iconCls : 'icon-add',
	width : 100,
	handler : function() {
		window_add_investment.show();
	}
});

var btn_edit_investment = new Ext.Button({
	text : '修改借款',
	iconCls : 'icon-edit',
	width : 100,
	handler : function() {
		var record = grid_investment.getSelectionModel().getSelected();
		if (record) {
			if(record.data.status=="未还"){
				window_edit_investment.show();
			}else{
				Ext.Msg.show({title : '错误提示',
							msg : '这笔投资已结清或已部分还贷,不能修改!',
							buttons : Ext.Msg.OK,
							icon : Ext.Msg.ERROR
							});
			}
		}
	}
});

var btn_intert_investment = new Ext.Button({
	text : '还息延期',
	iconCls : 'icon-edit',
	width : 100,
	handler : function() {
		var record = grid_investment.getSelectionModel().getSelected();
		if (record) {
			if(record.data.delaycos>0){
				Ext.Msg.show({title : '错误提示',
							msg : '这笔投资没有逾期!',
							buttons : Ext.Msg.OK,
							icon : Ext.Msg.ERROR
							});
			}else{
				window_intert_investment.show();
				
			}
		}
	}
});

var btn_part_investment = new Ext.Button({
	text : '归还部分借款',
	iconCls : 'icon-add',
	width : 100,
	handler : function() {
		var record = grid_investment.getSelectionModel().getSelected();
		if (record) {
			if(record.data.status=="已结清"){
				Ext.Msg.show({title : '错误提示',
							msg : '这笔投资已结清,不需要再还款!',
							buttons : Ext.Msg.OK,
							icon : Ext.Msg.ERROR
							});
			}else{
				window_part_investment.show();
				
			}
		}
	}
});


var btn_del_investment = new Ext.Button({
	text : '删除借款',
	iconCls : 'icon-del',
	width : 100,
	handler : function() {
		var record = grid_investment.getSelectionModel().getSelected();
		if (record) {
			if(record.data.status!="未还"){
				Ext.Msg.show({title : '错误提示',
							msg : '这笔投资已结清或已部分还款,不能删除!',
							buttons : Ext.Msg.OK,
							icon : Ext.Msg.ERROR
							});
				
			}else{
				Ext.Msg.confirm('确认删除这笔借款', '你确定删除该条记录?', function(btn) {
					if (btn == 'yes') {
						Ext.Ajax.request({
							url : '/investment/deleteInvestment.action',
							params : {tid : record.data.tid},
							success : function() {grid_investment.getStore().remove(record);
							
							},
							failure : function() {
								Ext.Msg.show({
									title : '错误提示',
									msg : '删除时发生错误,这笔投资已结清或您没有删除的权限!',
									buttons : Ext.Msg.OK,
									icon : Ext.Msg.ERROR
								});
							}
						});
					}
				});
			}
		}
	}
});

var btn_giveback_investment = new Ext.Button({
	text : '归还全部借款',
	iconCls : 'icon-edit',
	width : 100,
	handler : function() {
		var record = grid_investment.getSelectionModel().getSelected();
			if (record) {
			if(record.data.status=="已结清"){
				Ext.Msg.show({title : '错误提示',
							msg : '这笔投资已结清,不需要再还款!',
							buttons : Ext.Msg.OK,
							icon : Ext.Msg.ERROR
							});
				}else{
					Ext.Msg.confirm('确认归还全部借款', '你确定归还全部借款?', function(btn) {
						if (btn == 'yes') {
							Ext.Ajax.request({
								url : '/investment/givebackInvestment.action',
								params : {tid : record.data.tid},
								success : function() {
									ds_investment.load({params : {start : 0,limit : 20}});},
								failure : function() {
									Ext.Msg.show({
										title : '错误提示',
										msg : '归还时发生错误,这笔借款已结清!',
										buttons : Ext.Msg.OK,
										icon : Ext.Msg.ERROR
									});
								}
							});
						}
				});
			}
		}
	}
});

var text_search_investment = new Ext.form.TextField({
	name : 'textSearchInvestment',
	width : 150,
	emptyText : '根据合同号模糊查询!',
	listeners : {
		'specialkey' : function(field, e) {
			if (e.getKey() == Ext.EventObject.ENTER) {
				searchInvestment();
			}
		}
	}
});

var awoke_search_investment = new Ext.form.ComboBox({
		mode : 'local',
		name : 'awokeInvest',
		width : 150,
		editable : false,
		store : new Ext.data.SimpleStore({
			data : [['全部',0],['逾期', -1],['五天内应收借款', 5], ['十天内应收借款',10],['二十天内应收借款',20],['三十天内应收借款',30]],
			fields : ['text', 'value']
		}),
		displayField : 'text',
		valueField : 'value',
		hiddenName:'awokeDays',
		mode : 'local',
		triggerAction : 'all',
		emptyText : '全部应收借款',
		listeners : {
		'select' : function(combo, record, index) {
			ds_investment.baseParams.awokeDays = awoke_search_investment.getValue();
			ds_investment.baseParams.conditions = text_search_investment.getValue(); 
			ds_investment.load({params : {start : 0,limit : 20}});
		}
	}
});

var searchInvestment = function() {
	ds_investment.baseParams.awokeDays = awoke_search_investment.getValue();
	ds_investment.baseParams.conditions = text_search_investment.getValue(); 
	ds_investment.load({params : {start : 0,limit : 20}});
}



var btn_search_investment = new Ext.Button({
	text : '查询',
	iconCls : 'icon-search',
	handler : searchInvestment
});

var ds_investment = new Ext.data.Store({
	url : '/investment/findAllInvestment.action',
	reader : new Ext.data.JsonReader({totalProperty : 'totalProperty',root : 'root'}, 
	[{name : 'tid',mapping : 'tid',type : 'int'}, 
	{name : 'pid',mapping : 'fid',type : 'int'}, 
	{name : 'cnumber',mapping : 'cnumber',type : 'string'}, 
	{name : 'cardnumber',mapping : 'cardnumber',type : 'string'}, 
	{name : 'csum',mapping : 'csum',type : 'float'},  
	{name : 'loanid',mapping : 'loanid',type : 'int'}, 
	{name : 'loanname',mapping : 'loanname',type : 'string'},  
	{name : 'rate',mapping : 'rate',type : 'float'}, 
	{name : 'interest',mapping : 'interest',type : 'float'},
	{name : 'charge',mapping : 'charge',type : 'float'},
	{name : 'debtorid',mapping : 'debtorid',type : 'int'},
	{name : 'debtorname',mapping : 'debtorname',type : 'string'},
	{name : 'startdate',mapping : 'startdate',type : 'string'},
	{name : 'closedate',mapping : 'closedate',type : 'string'},
	{name : 'createtime',mapping : 'createtime',type : 'string'},
	{name : 'updatetime',mapping : 'updatetime',type : 'string'},	
	{name : 'accountid',mapping : 'accountid',type : 'int'},
	{name : 'accountname',mapping : 'accountname',type : 'string'},
	{name : 'balance',mapping : 'balance',type : 'float'},
	{name : 'status',mapping : 'status',type : 'string'},
	{name : 'createtime',type : 'string'},
	{name : 'delaydays',mapping : 'delaydays',type : 'int'},
	{name : 'delaycost',mapping : 'delaycost',type : 'float'},
	{name : 'memo',type : 'string'}
	])
});

function renderDelay(val){
	if(val > 0){
        return '<span style="color:red;">' + val + '</span>';
    }
    return val;
}

var grid_investment = new Ext.grid.EditorGridPanel({
	title : '借款管理',
	iconCls : 'icon-grid',
	region: 'center',
	loadMask : {msg : '数据加载中...'},
	width:400,
	cm : cm_investment,
	ds : ds_investment,
	sm : new Ext.grid.RowSelectionModel({singleSelect : true}),
	enableColumnMove : false,
	trackMouseOver : false,
	frame : true,
	clicksToEdit : 1,
	tbar : [btn_add_investment,btn_edit_investment, btn_del_investment,
			'-',btn_giveback_investment,btn_intert_investment,btn_part_investment,
			'-',text_search_investment,'-',awoke_search_investment,'-', btn_search_investment],
	
	bbar : new Ext.PagingToolbar({
		pageSize : 20,
		store : ds_investment,
		displayInfo : true,
		displayMsg : '第 {0} - {1} 条  共 {2} 条',
		emptyMsg : "没有记录",
		items:[{   
                text: '导出到Excel',   
                tooltip: 'Excel',   
                handler: investmentExport // 
                        }]
	}),listeners : {
		'click' : function(){
			var record = this.getSelectionModel().getSelected();
			tpl.overwrite(investment_Detail.body,record.data);
			ds_invest_data.baseParams.tid = record.data.tid; 
			ds_invest_data.load({params : {start : 0,limit : 1000}});
		}
	}
});

function investmentExport(){
	doExport(grid_investment);
}

var window_add_investment = new Ext.Window({
	title : '增加借款',
	width : 320,
	resizable : false,
	autoHeight : true,
	modal : true,
	closeAction : 'hide',
	listeners : {
		'hide' : function() {
			this.setTitle('增加借款');
			this.findById('investment.startdate').ownerCt.form.reset();
		}
	},
	items : [new Ext.FormPanel({
		border : false,
		baseCls : 'x-plain',
		bodyStyle : 'padding:5px 5px 0',
		labelAlign : 'right',
		labelWidth : 70,
		defaults : {anchor : '93%',msgTarget : 'side'},
		defaultType : 'textfield',
		items : [{
		   	xtype : 'combo',
			fieldLabel : '借款人',
			name : 'loanid',
			hiddenName : 'investment.loanid',
			valueField : 'tid',
		    displayField : 'name',
		    mode : 'remote',
			store : ds_custom_select,
			selectOnFocus : true,
			editable : false,
			allowBlank : false,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择借款人',
			listeners : {
				'select' : function(combo, record, index) {
					this.ownerCt.ownerCt.form.findField('investment.loanname').setValue(record.data.name);
				},
				'expand' : function(){
					ds_loan_select.load();
				}
			}
		   }, {xtype : 'hidden',name : 'investment.loanname'}
		   , {
		   	xtype : 'datefield',
			fieldLabel : '借款日期',
			id : 'investment.startdate',
			name : 'investment.startdate',
			format : 'Y-m-d',
			hiddenName : 'investment.startdate',
			allowBlank : false
		}, {
		   	xtype : 'numberfield',
			fieldLabel : '借款天数',
			id : 'ttdays',
			name : 'ttdays',
			allowBlank : false,
			listeners : {
				'blur' : function(){
					var start = this.ownerCt.ownerCt.form.findField('investment.startdate').value;
					var ttd = this.value;
					if(ttd!=''){
						this.ownerCt.ownerCt.form.findField('investment.closedate').setValue(DateUtil.AddDayByDate(start,ttd));
					}
					
				}
			}
		}, {
		   	xtype : 'datefield',
			fieldLabel : '到期日期',
			id : 'investment.closedate',
			name : 'investment.closedate',
			hiddenName : 'investment.closedate',
			readOnly : true,
			editable : false,
			format : 'Y-m-d',
			allowBlank : false
		},{
		   	xtype : 'combo',
			fieldLabel : '所属帐号',
			name : 'cardnumber',
			hiddenName : 'investment.cardnumber',
			valueField : 'cardnumber',
		    displayField : 'cardnumber',
		    mode : 'remote',
			store : ds_account_select,
			selectOnFocus : true,
			editable : true,
			allowBlank : false,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择所有帐号',
			listeners : {
				'select' : function(combo, record, index) {
					this.ownerCt.ownerCt.form.findField('investment.accountname').setValue(record.data.username);
					this.ownerCt.ownerCt.form.findField('investment.accountid').setValue(record.data.tid);
					this.ownerCt.ownerCt.form.findField('investment.debtorname').setValue(record.data.name);
					this.ownerCt.ownerCt.form.findField('investment.debtorid').setValue(record.data.owner);
					this.ownerCt.ownerCt.form.findField('bala').setValue(record.data.balance);
				},
				'expand' : function(){
					ds_account_select.load();
				}
			}
		   },
		    {
		    fieldLabel : '账户开户人',
			id : 'investment.accountname',
			name : 'investment.accountname',
			allowBlank : false,
			readOnly:true},
		    {xtype : 'hidden',name : 'investment.accountid'}, 
		    {fieldLabel : '贷款单位',
			id : 'investment.debtorname',
			name : 'investment.debtorname',
			allowBlank : false,
			readOnly:true},
		    {xtype : 'hidden',name : 'investment.debtorid'}, 
		    {
		  	xtype:'numberfield',
		 	fieldLabel : '账户余额',
		 	name : 'bala',
		 	id : 'bala',
		 	allowBlank : false,
		 	minLength : 1,
		 	readOnly:true,
		 	maxLength : 12
		  },
		   {
		  	xtype:'numberfield',
		 	fieldLabel : '借款金额',
		 	name : 'investment.csum',
		 	allowBlank : false,
		 	vtype:'balance',//自定义的验证类型
            vtypeText:'超过账户所剩余额',
            confirmTo:'bala',//要比较的另外一个的组件的id
		 	minLength : 1,
		 	maxLength : 12
		  },
		  {
		  	xtype:'numberfield',
		 	fieldLabel : '利率%',
		 	name : 'investment.rate',
		 	allowBlank : false,
		 	minLength : 1,
		 	maxLength : 12
		  },
		  {
		  	xtype:'numberfield',
		 	fieldLabel : '投资费用',
		 	name : 'investment.charge',
		 	allowBlank : false,
		 	minLength : 1,
		 	maxLength : 12
		  },
		   {xtype: 'textarea',
		   	fieldLabel : '备注',
		   name : 'investment.memo',
		   maxLength : 60
		   }
		],
		buttonAlign : 'right',
		minButtonWidth : 60,
		buttons : [{
			text : '添加',
			handler : function(btn) {
				var frm = this.ownerCt.ownerCt.form;
				if (frm.isValid()) {
					btn.disable();
					var unfield = frm.findField('investment.cardnumber');
					frm.submit({
						waitTitle : '请稍候',
						url : '/investment/saveInvestment.action',
						waitMsg : '正在提交表单数据,请稍候...',
						success : function(form, action) {
							btn.enable();
							window_add_investment.hide();
							ds_investment.load({params : {start : 0,limit : 20}});
						},
						failure : function() {
							Ext.Msg.show({
								title : '错误提示',
								msg : '发生不明错误，请与管理员联系!',
								buttons : Ext.Msg.OK,
								fn : function() {
									unfield.focus(true);
									btn.enable();
								},
								icon : Ext.Msg.ERROR
							});
						}
					});
				}
			}
		}, {text : '重置',handler : function() {this.ownerCt.ownerCt.form.reset();}
		}, {text : '取消',handler : function() {
			this.ownerCt.ownerCt.ownerCt.hide();
			ds_investment.load({params : {start : 0,limit : 20}});
		}
		}]
	})]
});

var window_edit_investment = new Ext.Window({
	title : '修改借款',
	width : 320,
	resizable : false,
	autoHeight : true,
	modal : true,
	closeAction : 'hide',
	listeners : {
		'hide' : function() {
			this.setTitle('修改借款');
			this.findById('investmentd.tid').ownerCt.form.reset();
		},
		'show' :function(){
			var record = grid_investment.getSelectionModel().getSelected();
			this.findById('investmentd.tid').setValue(record.data.tid);
			this.findById('investloanname').setValue(record.data.loanname);
			this.findById('investmentd.cnumber').setValue(record.data.cnumber);
			this.findById('investmentd.loanid').setValue(record.data.loanid);
			this.findById('investmentd.startdate').setValue(renderDate(record.data.startdate));
			this.findById('investmentd.closedate').setValue(renderDate(record.data.closedate));
			this.findById('investcardnumber').setValue(record.data.cardnumber);
			this.findById('investmentd.accountname').setValue(record.data.accountname);
			this.findById('investmentd.accountid').setValue(record.data.accountid);
			this.findById('investmentd.debtorname').setValue(record.data.debtorname);
			this.findById('investmentd.debtorid').setValue(record.data.debtorid);
			this.findById('balad').setValue(record.data.balance+record.data.csum);
			this.findById('investmentd.csum').setValue(record.data.csum);
			this.findById('investmentd.rate').setValue(record.data.rate);
			this.findById('investmentd.charge').setValue(record.data.charge);
			this.findById('investmentd.memo').setValue(record.data.memo);
		}
	},
	items : [new Ext.FormPanel({
		border : false,
		baseCls : 'x-plain',
		bodyStyle : 'padding:5px 5px 0',
		labelAlign : 'right',
		labelWidth : 70,
		defaults : {anchor : '93%',msgTarget : 'side'},
		defaultType : 'textfield',
		items : [{xtype : 'hidden',id:'investmentd.tid',name : 'investment.tid'},
			{ fieldLabel : '合同号',
			id : 'investmentd.cnumber',
			name : 'investment.cnumber',
			allowBlank : false,
			readOnly :true,
			minLength : 5,
		 	maxLength : 32},{
		   	xtype : 'combo',
			fieldLabel : '借款人',
			id:'investloanname',
			name : 'investloanname',
			hiddenName : 'investment.loanname',
			valueField : 'name',
		    displayField : 'name',
		    mode : 'remote',
			store : ds_custom_select,
			selectOnFocus : true,
			editable : false,
			allowBlank : false,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择借款人',
			listeners : {
				'select' : function(combo, record, index) {
					this.ownerCt.ownerCt.form.findField('investment.loanid').setValue(record.data.tid);
				},
				'expand' : function(){
					ds_loan_select.load();
				}
			}
		   }, {xtype : 'hidden',id:'investmentd.loanid',name : 'investment.loanid'}
		   , {
		   	xtype : 'datefield',
			fieldLabel : '借款日期',
			id : 'investmentd.startdate',
			name : 'investment.startdate',
			format : 'Y-m-d',
			hiddenName : 'investment.startdate',
			allowBlank : false
		}, {
		   	xtype : 'datefield',
			fieldLabel : '到期日期',
			id : 'investmentd.closedate',
			name : 'investment.closedate',
			hiddenName : 'investment.closedate',
			vtype:'dttypes',//自定义的验证类型
            vtypeText:'到期日期应该比贷款日期迟',
            confirmTo:'investmentd.startdate',//要比较的另外一个的组件的id
			format : 'Y-m-d',
			allowBlank : false
		},{
		   	xtype : 'combo',
			fieldLabel : '所属帐号',
			id:'investcardnumber',
			name : 'investcardnumber',
			hiddenName : 'investment.cardnumber',
			valueField : 'cardnumber',
		    displayField : 'cardnumber',
		    mode : 'remote',
			store : ds_account_select,
			selectOnFocus : true,
			editable : true,
			allowBlank : false,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择所有帐号',
			listeners : {
				'select' : function(combo, record, index) {
					this.ownerCt.ownerCt.form.findField('investment.accountname').setValue(record.data.username);
					this.ownerCt.ownerCt.form.findField('investment.accountid').setValue(record.data.tid);
					this.ownerCt.ownerCt.form.findField('investment.debtorname').setValue(record.data.name);
					this.ownerCt.ownerCt.form.findField('investment.debtorid').setValue(record.data.owner);
					this.ownerCt.ownerCt.form.findField('bala').setValue(record.data.balance);
				},
				'expand' : function(){
					ds_account_select.load();
				}
			}
		   },
		    {
		    fieldLabel : '账户开户人',
			id : 'investmentd.accountname',
			name : 'investment.accountname',
			allowBlank : false,
			readOnly:true},
		    {xtype : 'hidden',id:'investmentd.accountid',name : 'investment.accountid'}, 
		    {fieldLabel : '贷款单位',
			id : 'investmentd.debtorname',
			name : 'investment.debtorname',
			allowBlank : false,
			readOnly:true},
		    {xtype : 'hidden',id:'investmentd.debtorid',name : 'investment.debtorid'}, 
		    {
		  	xtype:'numberfield',
		 	fieldLabel : '账户余额',
		 	name : 'bala',
		 	id : 'balad',
		 	allowBlank : false,
		 	minLength : 1,
		 	readOnly:true,
		 	maxLength : 12
		  },
		   {
		  	xtype:'numberfield',
		 	fieldLabel : '借款金额',
		 	id:'investmentd.csum',
		 	name : 'investment.csum',
		 	allowBlank : false,
		 	vtype:'balance',//自定义的验证类型
            vtypeText:'超过账户所剩余额',
            confirmTo:'balad',//要比较的另外一个的组件的id
		 	minLength : 1,
		 	maxLength : 12
		  },
		  {
		  	xtype:'numberfield',
		 	fieldLabel : '利率%',
		 	id : 'investmentd.rate',
		 	name : 'investment.rate',
		 	allowBlank : false,
		 	minLength : 1,
		 	maxLength : 12
		  },
		  {
		  	xtype:'numberfield',
		 	fieldLabel : '投资费用',
		 	id : 'investmentd.charge',
		 	name : 'investment.charge',
		 	allowBlank : false,
		 	minLength : 1,
		 	maxLength : 12
		  },
		   {xtype: 'textarea',
		   	fieldLabel : '备注',
		   id : 'investmentd.memo',
		   name : 'investment.memo',
		   maxLength : 60
		   }
		],
		buttonAlign : 'right',
		minButtonWidth : 60,
		buttons : [{
			text : '保存',
			handler : function(btn) {
				var frm = this.ownerCt.ownerCt.form;
				if (frm.isValid()) {
					btn.disable();
					var unfield = frm.findField('investment.cardnumber');
					frm.submit({
						waitTitle : '请稍候',
						url : '/investment/updateInvestment.action',
						waitMsg : '正在提交表单数据,请稍候...',
						success : function(form, action) {
							btn.enable();
							window_edit_investment.hide();
							ds_investment.load({params : {start : 0,limit : 20}});
						},
						failure : function() {
							Ext.Msg.show({
								title : '错误提示',
								msg : '发生不明错误，请与管理员联系!',
								buttons : Ext.Msg.OK,
								fn : function() {
									unfield.focus(true);
									btn.enable();
								},
								icon : Ext.Msg.ERROR
							});
						}
					});
				}
			}
		},{text : '取消',handler : function() {
			this.ownerCt.ownerCt.ownerCt.hide();
			ds_investment.load({params : {start : 0,limit : 20}});
		}
		}]
	})]
});

var window_part_investment = new Ext.Window({
	title : '还款',
	width : 320,
	resizable : false,
	autoHeight : true,
	modal : true,
	closeAction : 'hide',
	listeners : {
		'hide' : function() {
			this.setTitle('还款');
			this.findById('hk.tid').ownerCt.form.reset();
		},
		'show' :function(){
			var record = grid_investment.getSelectionModel().getSelected();
			this.findById('hk.tid').setValue(record.data.tid);
			this.findById('hk.loanname').setValue(record.data.loanname);
			this.findById('hk.cnumber').setValue(record.data.cnumber);
			this.findById('hk.csum').setValue(record.data.csum);
			this.findById('hk.rate').setValue(record.data.rate);
			this.findById('hk.money').setValue(0);
			this.findById('hk.charge').setValue(0);
			
		}
	},
	items : [new Ext.FormPanel({
		border : false,
		baseCls : 'x-plain',
		bodyStyle : 'padding:5px 5px 0',
		labelAlign : 'right',
		labelWidth : 70,
		defaults : {anchor : '93%',msgTarget : 'side'},
		defaultType : 'textfield',
		items : [{xtype : 'hidden',id:'hk.tid',name : 'investment.tid'},
			{ fieldLabel : '合同号',
			id : 'hk.cnumber',
			name : 'investment.cnumber',
			allowBlank : false,
			readOnly :true,
			minLength : 5,
		 	maxLength : 32},
		 	{ fieldLabel : '还款人',
			id : 'hk.loanname',
			name : 'investment.loanname',
			allowBlank : false,
			readOnly :true,
			minLength : 1,
		 	maxLength : 32},
		   {
		  	xtype:'numberfield',
		 	fieldLabel : '需还金额',
		 	id:'hk.csum',
		 	name : 'hk.csum',
		 	allowBlank : false,
		 	readOnly :true,
		 	minLength : 1,
		 	maxLength : 12
		  },
		  {
		  	xtype:'numberfield',
		 	fieldLabel : '本次归还金额',
		 	id:'hk.money',
		 	name : 'investment.csum',
		 	allowBlank : false,
		 	vtype:'balance',//自定义的验证类型
            vtypeText:'超过所借的金额',
            confirmTo:'hk.csum',//要比较的另外一个的组件的id
		 	minLength : 1,
		 	maxLength : 12
		  },
		  {
		  	xtype:'numberfield',
		 	fieldLabel : '调整利率%',
		 	id : 'hk.rate',
		 	name : 'investment.rate',
		 	allowBlank : false,
		 	minLength : 1,
		 	maxLength : 12
		  }, {
		   	xtype : 'datefield',
			fieldLabel : '续借日期',
			id : 'hk.startdate',
			name : 'investment.startdate',
			format : 'Y-m-d',
			hiddenName : 'investment.startdate',
			allowBlank : false
		}, {
		   	xtype : 'numberfield',
			fieldLabel : '续借天数',
			id : 'hkttdays',
			name : 'ttdays',
			allowBlank : false,
			listeners : {
				'blur' : function(){
					var start = this.ownerCt.ownerCt.form.findField('investment.startdate').value;
					var ttd = this.value;
					if(ttd!=''){
						this.ownerCt.ownerCt.form.findField('investment.closedate').setValue(DateUtil.AddDayByDate(start,ttd));
					}
					
				}
			}
		}, {
		   	xtype : 'datefield',
			fieldLabel : '到期日期',
			id : 'hk.closedate',
			name : 'investment.closedate',
			hiddenName : 'investment.closedate',
			readOnly : true,
			editable : false,
			format : 'Y-m-d',
			allowBlank : false
		},
		  {
		  	xtype:'numberfield',
		 	fieldLabel : '投资费用',
		 	id : 'hk.charge',
		 	name : 'investment.charge',
		 	allowBlank : false,
		 	minLength : 1,
		 	maxLength : 12
		  },
		   {xtype: 'textarea',
		   	fieldLabel : '备注',
		   id : 'hk.memo',
		   name : 'investment.memo',
		   maxLength : 120
		   }
		],
		buttonAlign : 'right',
		minButtonWidth : 60,
		buttons : [{
			text : '还款',
			handler : function(btn) {
				var frm = this.ownerCt.ownerCt.form;
				if (frm.isValid()) {
					btn.disable();
					var unfield = frm.findField('hk.tid');
					frm.submit({
						waitTitle : '请稍候',
						url : '/investment/givebackPartInvestment.action',
						waitMsg : '正在提交表单数据,请稍候...',
						success : function(form, action) {
							btn.enable();
							window_part_investment.hide();
							ds_investment.load({params : {start : 0,limit : 20}});
						},
						failure : function() {
							Ext.Msg.show({
								title : '错误提示',
								msg : '发生不明错误，请与管理员联系!',
								buttons : Ext.Msg.OK,
								fn : function() {
									unfield.focus(true);
									btn.enable();
								},
								icon : Ext.Msg.ERROR
							});
						}
					});
				}
			}
		},{text : '取消',handler : function() {
			this.ownerCt.ownerCt.ownerCt.hide();
			ds_investment.load({params : {start : 0,limit : 20}});
		}
		}]
	})]
});

var window_intert_investment = new Ext.Window({
	title : '还息延期',
	width : 320,
	resizable : false,
	autoHeight : true,
	modal : true,
	closeAction : 'hide',
	listeners : {
		'hide' : function() {
			this.setTitle('还息延期');
			this.findById('hx.tid').ownerCt.form.reset();
		},
		'show' :function(){
			var record = grid_investment.getSelectionModel().getSelected();
			this.findById('hx.tid').setValue(record.data.tid);
			this.findById('hx.loanname').setValue(record.data.loanname);
			this.findById('hx.cnumber').setValue(record.data.cnumber);
			this.findById('hx.delaycost').setValue(record.data.delaycost);
			this.findById('hx.lixi').setValue(0);
		}
	},
	items : [new Ext.FormPanel({
		border : false,
		baseCls : 'x-plain',
		bodyStyle : 'padding:5px 5px 0',
		labelAlign : 'right',
		labelWidth : 70,
		defaults : {anchor : '93%',msgTarget : 'side'},
		defaultType : 'textfield',
		items : [{xtype : 'hidden',id:'hx.tid',name : 'investment.tid'},
			{ fieldLabel : '合同号',
			id : 'hx.cnumber',
			name : 'investment.cnumber',
			allowBlank : false,
			readOnly :true,
			minLength : 5,
		 	maxLength : 32},
		 	{ fieldLabel : '还款人',
			id : 'hx.loanname',
			name : 'investment.loanname',
			allowBlank : false,
			readOnly :true,
			minLength : 1,
		 	maxLength : 32},
		  {
		  	xtype:'numberfield',
		 	fieldLabel : '逾期利息',
		 	id:'hx.delaycost',
		 	name : 'hx.interest',
		 	allowBlank : false,
		 	readOnly :true,
		 	minLength : 1,
		 	maxLength : 12
		  },
		  {
		  	xtype:'numberfield',
		 	fieldLabel : '归还利息',
		 	id:'hx.lixi',
		 	name : 'investment.interest',
		 	allowBlank : false,
		 	//vtype:'balance',//自定义的验证类型
            vtypeText:'超过所需支付的利息',
            //confirmTo:'hx.delaycost',//要比较的另外一个的组件的id
		 	minLength : 1,
		 	maxLength : 12
		  }
		],
		buttonAlign : 'right',
		minButtonWidth : 60,
		buttons : [{
			text : '归还利息',
			handler : function(btn) {
				var frm = this.ownerCt.ownerCt.form;
				if (frm.isValid()) {
					btn.disable();
					var unfield = frm.findField('hx.tid');
					frm.submit({
						waitTitle : '请稍候',
						url : '/investment/givebackIntertInvestment.action',
						waitMsg : '正在提交表单数据,请稍候...',
						success : function(form, action) {
							btn.enable();
							window_intert_investment.hide();
							ds_investment.load({params : {start : 0,limit : 20}});
						},
						failure : function() {
							Ext.Msg.show({
								title : '错误提示',
								msg : '发生不明错误，请与管理员联系!',
								buttons : Ext.Msg.OK,
								fn : function() {
									unfield.focus(true);
									btn.enable();
								},
								icon : Ext.Msg.ERROR
							});
						}
					});
				}
			}
		},{text : '取消',handler : function() {
			this.ownerCt.ownerCt.ownerCt.hide();
			ds_investment.load({params : {start : 0,limit : 20}});
		}
		}]
	})]
});

var investment_Detail = new Ext.Panel({
    title: '详细内容',//标题
    collapsible:true,//右上角上的那个收缩按钮，设为false则不显示
    region: 'south',
    width:400,
    height:400
});
var cm_invest_detail = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
	header : '合同号',
	width : 120,
	dataIndex : 'cnumber'
},{
	header : '借款人',
	width : 80,
	dataIndex : 'loanname',
	sortable : false
},{
	header : '归还金额',
	width : 80,
	dataIndex : 'csum',
	sortable : false
},{
	header : '归还利息',
	width : 60,
	dataIndex : 'interest',
	renderer:renderDelay,
	sortable : true
},{
	header : '投资费用',
	width : 60,
	dataIndex : 'charge',
	renderer:renderDelay,
	sortable : true
},{
	header : '归还日期',
	width : 90,
	dataIndex : 'createtime',
	renderer: renderDate,
	sortable : true
}]);

cm_invest_detail.defaultSortable = true;
var grid_invest_detail = new Ext.grid.EditorGridPanel({
	title : '还款还息纪录',
	iconCls : 'icon-grid',
	region: 'center',
	loadMask : {msg : '数据加载中...'},
	width:400,
	cm : cm_invest_detail,
	ds : ds_invest_data,
	sm : new Ext.grid.RowSelectionModel({singleSelect : true}),
	enableColumnMove : false,
	trackMouseOver : false,
	frame : true,
	clicksToEdit : 1
});
    
var investment_tabs=new Ext.TabPanel({
        activeTab:0,
        region: 'south',
        height:300,
        frame:true,
        items:[investment_Detail,grid_invest_detail],
        listeners : {
		'tabchange ' : function(tabs,tab){
			//var record = this.getSelectionModel().getSelected();
			//ds_invest_data.baseParams.tid = record.data.tid(); 
			//ds_invest_data.load({params : {start : 0,limit : 1000}});
		}
	}
 });
var p_investment = {
	id : 'investment-panel',
	border : false,
	layout : 'border',
	items : [grid_investment,investment_tabs]
};

